<?php if(isset($query_error)):?>
	<p class="query_error"><?php echo $query_error?></p>
<?php endif?>
<?php if (isset($ask_result)):?>
<div>
	<h1>ASK Result</h1>
	<strong class="ask_result"><?php echo $ask_result?></strong>
</div>
<?php endif?>

<?php if (isset($select_array)):?>
<div>
	<h1>SELECT Results</h1>
	<table class="select_result">
		<tr>
			<?php foreach($select_array[0] as $k => $v):?>
				<th><?php echo $k?></th>
			<?php endforeach?>
		</tr>
	<?php foreach($select_array as $row):?>
		<tr>
			<?php foreach($row as $k => $v):?>
				<td>
					<?php echo $v['value']?>
				</td>
			<?php endforeach?>
		</tr>
	<?php endforeach?>
	</table>
</div>
<?php endif?>


	<?php if(!empty($resources)):?>
	<ol>
	<?php foreach($resources as $uri => $properties):?>
		<li>
			<?php
			$Res = $Resources->to_resource($uri);
			?>
			<h2 xml:lang="<?php echo $this->language?>">
				<?php echo $Res->get_title()?>	
			</h2>
			<a href="/?uri=<?php echo urlencode($uri)?>"><span>convert(<?php echo $uri?>)</span></a>
			<a href="?query=<?php echo urlencode("DESCRIBE <".$uri.">")?>"><span>DESCRIBE &lt;<?php echo $uri?>&gt;</span></a>
			<?php foreach($Res->properties() as $property => $objects):?>
				<dt title="
				<?php echo $property ?>"><?php echo $this->Convert->URI->to_term($property)?></dt>

				<?php foreach($objects as $o):?>
					<dd<?php if(isset($o['lang'])):?>lang="<?php echo $o['lang']?>"<?php endif?>>
					<?php if (preg_match('/(jpg)|(gif)|(png)/', $o['value']) || preg_match('/(img)|(depiction)|(pic)$/', $property)):?>
						<img alt="image" src="<?php  echo $o['value'] ?>"/>
					<?php else:?>		
						<?php if(isset($o['type']) 
						and isset($o['value']) 
						and $o['type']=='uri'):?>
							<?php if( $Resources->has_resource($o['value']) ): ?>
								<a rel="<?php echo $this->Convert->Qname->to_eRDF_term($p)?>"
									href="/Res/?uri=<?php echo urlencode($o['value'])?>#res"><?php echo $Resources->to_resource($o['value'])->get_title() ?></a>
							<?php else:?>
								<a rel="<?php echo $this->Convert->Qname->to_eRDF_term($p)?>"
									href="<?php echo $o['value']; ?>"><?php echo $o['value']; ?></a>
							<?php endif; ?>
						<?php elseif(isset($o['value'])):?>
							<span class="<?php echo $this->Convert->Qname->to_eRDF_term($p)?>"><?php echo $o['value']?></span>
						<?php endif?>
						<?php if(isset($o['lang'])):?><span class="lang_label"><?php echo $o['lang']?></span><?php endif?>
					<?php endif?>
					</dd>
					
				<?php endforeach?>
				
			<?php endforeach ?>	
		</li>
	<?php endforeach?>
	</ol>
	<?php elseif(empty($resources) && isset($resources) && !isset($query_error) ):?>
		<p>
			There were no matching results - sorry.
		</p>
	<?php endif?>
	
<form method="get" accept-charset="utf-8">
	<label>Sparql:
		<textarea class="sparql" rows="20" cols="40" name="query"><?php if ( isset($_GET['query']) ): echo stripslashes(htmlentities($_GET['query']));?>
		<?php else:?>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
<?php endif?></textarea>
	</label>
	<fieldset>
	<label>Output:
		<select name="output" id="output-select">
		<optgroup label="All Result Formats">		
			<option value="html">HTML</option>
			<option value="phps">PHP</option>
			<option value="json">JSON</option>
			<option value="jsonp">JSONP</option>		
		</optgroup>	

		<optgroup label="CONSTRUCT and DESCRIBE" id="rdf-only">
			<option value="rdf">RDF/XML</option>
			<option value="rss">RSS 1.0</option>
			<option value="turtle">Turtle</option>
			<option value="trix">TRIX</option>
			<option value="exhibit">Exhibit (HTML)</option>
			<option value="exhibit-json">Exhibit JSON</option>
			<option value="exhibit-jsonp">Exhibit JSONP</option>
		</optgroup>	
		</select>
	</label>
	<label class="jsonp">JSONP Callback: <input type="text" name="callback" value="<?php h($_GET['callback']) ?>" /></label>
	</fieldset>
	
	<p><input type="submit" value="Query &rarr;"></p>
</form>
